package org.jbit.buy.daoimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.jbit.buy.dao.BaseDao;
import org.jbit.buy.dao.UserDao;
import org.jbit.buy.entity.User;

public class UserDaoImpl extends BaseDao implements UserDao{
//  查找loginname是否重名
	public User findUser(String loginName) {
		 User user=null;
	        ResultSet rs = null;
	        String sql = "select * from easybuy_user where loginName=?";
	        try {
	            rs = this.exxcuteQuery(sql,loginName);
	            while (rs.next()) {
	            	user=new User();
	            	 user.setLoginName(loginName);
	             
	            }
	        } catch (SQLException e) {
	            e.printStackTrace();
	           
	        } finally{
	        	closeAll(connection, pstmt, rs); 	
	        }
	        return user;
	}

    //保存用户注册信息	
	public int saveUser(User user) {
	        ResultSet rs = null;
	        String sql = "insert into easybuy_user(loginName,userName,password,sex,identityCode,email,mobile,type) values(?,?,?,?,?,?,?,?)";
	        Object[] params = new Object[] {user.getLoginName(),user.getUserName(),user.getPassword(),user.getSex(),user.getIdentityCode(),user.getEmail(),user.getMobile(),user.getType()};
	         int result = 0;
	            try {
	               result = executeUpdate(sql, params);
	        } catch (Exception e) {
	            e.printStackTrace();
	           
	        } finally{
	        	closeAll(connection, pstmt, rs); 	
	        	 return result;
	        }
	       
	}
	
	
	
	
	public static void main(String[] args) {
		UserDao uu=new UserDaoImpl();
     	User us=new User();
//     	us.setLoginName("zhangheng");
//     	us.setUserName("张衡");
//     	us.setPassword("15349787");
//     	us.setSex(1);
//     	us.setIdentityCode(320830199);
//		us.setEmail("15349787@qq.com");
//		us.setMobile("15250855233");
//		us.setType(1);
//		int a=uu.saveUser(us);
//		if(a>0){
//			System.out.println("注册成功");
//		}else{
//			System.out.println("注册失败");
//		}
     	
     	
     	
//     User uss=uu.findUser("admin");
//     if(uss==null){
//    	 System.out.println("不存在");
//     }else{
//    	 System.out.println("存在");
//     }
     User uss=uu.findUser("liguangliang","25f9e794323b453885f5181f1b624d0");
     if(uss==null){
    	 System.out.println("用户不存在");
     }else{
    	 System.out.println("登陆成功");
     }
	}

	  //登录查询
	public User findUser(String loginName, String password) {
		User user=null;
        ResultSet rs = null;
        String sql = "select * from easybuy_user where loginName=? and password=?";
        try {
            rs = this.exxcuteQuery(sql,loginName,password);
            while (rs.next()) {
            	user=new User();
            	 user.setLoginName(loginName);
                 user.setPassword(password);
            }
        } catch (SQLException e) {
            e.printStackTrace();
           
        } finally{
        	closeAll(connection, pstmt, rs); 	
        }
        return user;
	}

}
